package org.arain.baseii.app.navigation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.arain.baseii.common.CommonServiceImpl;
import org.arain.baseii.grid.GridData;
import org.arain.baseii.grid.QueryParam;
import org.arain.baseii.mapper.BlogNavigationMapper;
import org.arain.baseii.pojo.BlogNavigation;
import org.arain.baseii.pojo.BlogNavigationExample;
import org.arain.baseii.pojo.BlogNavigationExample.Criteria;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.arain.baseii.utils.DictPojo;
import org.arain.baseii.utils.DictUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;

/**
 * 
* <p>Title:NavigationServiceImpl </p>
* @author Arain
* @date2017年10月31日
 */
@Service
public class NavigationServiceImpl extends CommonServiceImpl<NavigationDto> implements NavigationService{
	@Autowired
	private BlogNavigationMapper blogNavigationMapper;
	@Override
	public ArainResult save_01(NavigationDto dto) {
		if(StringUtils.isNotBlank(dto.getId())) {
			BlogNavigation navigation = new BlogNavigation();
			AppUtils.copyProperties(navigation, dto);
			blogNavigationMapper.updateByPrimaryKey(navigation);
			return ArainResult.ok();
		} else  {
			BlogNavigation navigation = new BlogNavigation();
			AppUtils.copyProperties(navigation, dto);
			blogNavigationMapper.insert(navigation);
			return ArainResult.ok();
		}
	}

	@Override
	public String load_grid(QueryParam param) {
		NavigationDto dto = new NavigationDto();
		try {
			dto = this.jsonToObject(param.getFilterRules(), dto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JSONArray array = new JSONArray();
		BlogNavigationExample example = new BlogNavigationExample();
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotBlank(param.getOrder()) && StringUtils.isNotBlank(param.getSort())) {
			example.setOrderByClause(param.getSort()+" "+param.getOrder());
		}
		if(StringUtils.isNotBlank(dto.getTitle())) {
			criteria.andTitleLike("%"+dto.getTitle()+"%");
		}
		if(StringUtils.isNotBlank(dto.getId())) {
			criteria.andPidEqualTo(Long.valueOf(dto.getId()));
		}
		PageHelper.startPage(param.getPage(), param.getRows());
		List<BlogNavigation> list = blogNavigationMapper.selectByExample(example);
		int countByExample = blogNavigationMapper.countByExample(example);
		for (BlogNavigation navigation : list) {
			JSONObject object = new JSONObject();
			object.put("id", navigation.getId());
			object.put("title", navigation.getTitle());
			object.put("url", navigation.getUrl());
			object.put("sort", navigation.getSort());
			object.put("enabled",  DictUtil.load_dict(navigation.getEnabled()+"", "ENABLED_").getDispaly());
			
			array.add(object);
		}
		GridData data = new GridData();
		data.setTotal(countByExample);
		data.setRows(array);
		return GridData.gridToJson(data);
	}

	@Override
	public NavigationDto load_01(NavigationDto dto) {
		BlogNavigation navigation = blogNavigationMapper.selectByPrimaryKey(Long.valueOf(dto.getId()));
		AppUtils.copyProperties(dto, navigation);
		return dto;
	}

	@Override
	public String load_pid(HttpServletRequest request) {
		JSONArray array = new JSONArray();
		JSONObject pid = new JSONObject();
		pid.put("id", 0);
		pid.put("title", "顶级导航");
		array.add(pid);
		BlogNavigationExample example = new BlogNavigationExample();
		example.createCriteria().andPidEqualTo((long) 0).andEnabledEqualTo((long) 1);
		List<BlogNavigation> list = blogNavigationMapper.selectByExample(example);
		for (BlogNavigation blogNavigation : list) {
			JSONObject object = new JSONObject();
			object.put("id", blogNavigation.getId());
			object.put("title", blogNavigation.getTitle());
			
			array.add(object);
		}
		return array.toString();
	}

	@Override
	public String load_type(HttpServletRequest request) {
		JSONArray array = new JSONArray();
		List<DictPojo> list = DictUtil.load_dict_list("Navigation_type");
		for (DictPojo dictPojo : list) {
			JSONObject object = new JSONObject();
			object.put("value", dictPojo.getValue());
			object.put("text", dictPojo.getDispaly());
			
			array.add(object);
		}
		return array.toString();
	}

	@Override
	public String load_pid_list(HttpServletRequest request) {
		com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
		JSONObject pid = new JSONObject();
		pid.put("id", 0);
		pid.put("text", "顶级导航");
		array.add(pid);
		BlogNavigationExample example = new BlogNavigationExample();
		example.createCriteria().andEnabledEqualTo((long) 1);
		List<BlogNavigation> list = blogNavigationMapper.selectByExample(example);
		for (BlogNavigation navigation : list) {
			JSONObject object = new JSONObject();
			object.put("id", navigation.getId());
			object.put("text", navigation.getTitle());
			object.put("pid", navigation.getPid());
			
			array.add(object);
		}
		return array.toString();
	}
	
	@Override
	public String load_list(HttpServletRequest request) {
		JSONArray array = new JSONArray();
		BlogNavigationExample example = new BlogNavigationExample();
		example.createCriteria().andEnabledEqualTo((long) 1).andTypeEqualTo((long) 1);
		List<BlogNavigation> list = blogNavigationMapper.selectByExample(example);
		for (BlogNavigation blogNavigation : list) {
			JSONObject object = new JSONObject();
			object.put("id", blogNavigation.getId());
			object.put("title", blogNavigation.getTitle());
			
			array.add(object);
		}
		return array.toString();
	}

}
